﻿@model Fish.Models.Customizeds

@{
    ViewBag.Title = "Create";
}




@{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>@ViewBag.TabName</h4>
        <hr />
        @Html.ValidationSummary(true)

        @for (var i = 0; i < Model.fieldTitle.Count; i++)
        {
            
            <div class="form-group">
                @Html.Label(Model.fieldTitle.ElementAt(i).text, new { @class = "control-label col-md-2" })
                @if (Model.fieldType.ElementAt(i).text.Equals("Text"))
                {

                <div class="col-md-10">
                    @{
                    var x = (string[])ViewBag.passedArraySent.ToString().Split(',');
                    }
                    @if (i < x.Length) { 
                        @Html.TextBox("input" + i, x[i])
                    }
                    else
                    {
                           @Html.TextBox("input" + i)

                    }
                </div>
                }
                @if (Model.fieldType.ElementAt(i).text.Equals("TextArea"))
                {

                <div class="col-md-10">
                    @{
                    var x = (List<String>)ViewBag.passedArraySent.ToString().Split(",").ToList();
                    }
                    
                    @Html.TextArea("input" + i, x[i])
                </div>
                }
            </div>
        }

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input id="createCustomized" type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to Event", "currentEvent", "EventInfo")
</div>
@section Scripts {
    <script>
        $(document).ready(function () {


            $("#createCustomized").click(function () {
                var customized = new Array();
                var i = 0;
                while (true) {
                    if (document.getElementById("input" + i) == null) {
                        break;
                    }
                    var custom = document.getElementById("input" + i).value;
                  //  alert(custom);
                    customized[i] = custom;
                    i++;
                }
                var idNumber = @Model.ID.ToString();
                //alert(idNumber);
                $.ajax({
                    type: "POST",
                    url: "/Customized/SaveCustomized",
                    dataType: "json",
                    contentType: 'application/json; charset=utf-8',//this is important!!
                    data: JSON.stringify({ customized: customized, cusNumber: idNumber }),
                    success: function (data, textStatus, xhr) {
                            window.location.href = "/";
                            //alert("Success");
                    },
                    error: function (xhr, ajaxOptions, error) {
                        //alert(xhr.status);
                        window.location.href =xhr.responseText;
                           // document.getElementById("hi").innerHTML = ('Error: ' + xhr.responseText);
                    },
                    traditional: true

                });
            });
        });
    </script>
    @Scripts.Render("~/bundles/jqueryval")
}
